package cn.hxlc.tie.demo.modular.ljt.equipment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hxlc.tie.demo.core.error.MdmExceptionEnum;
import cn.hxlc.tie.demo.modular.ljt.equipment.entity.EquipmentCategory;
import cn.hxlc.tie.demo.modular.ljt.equipment.entity.Facility;
import cn.hxlc.tie.demo.modular.ljt.equipment.mapper.EquipmentCategoryMapper_lt;
import cn.hxlc.tie.demo.modular.ljt.equipment.mapper.FacilityMapper_lt;
import cn.hxlc.tie.demo.modular.ljt.equipment.model.params.EquipmentGroupParam;
import cn.hxlc.tie.demo.modular.ljt.equipment.model.result.EquipmentGroupResult;
import cn.hxlc.tie.demo.modular.ljt.equipment.service.EquipmentCategoryService_lt;
import cn.hxlc.tie.demo.modular.ljt.equipment.util.NumberUtil;
import cn.hxlc.tie.libs.mp.page.PageFactory;
import cn.hxlc.tie.model.exp.ServiceException;
import cn.hxlc.tie.model.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class EquipmentCategoryServiceImpl_lt extends ServiceImpl<EquipmentCategoryMapper_lt, EquipmentCategory> implements EquipmentCategoryService_lt {

    @Autowired
    private EquipmentCategoryMapper_lt equipmentCategoryMapper;

    @Autowired
    private FacilityMapper_lt facilityMapper;

    @Override
    public PageResult<EquipmentGroupResult> findPageBySpec(EquipmentGroupParam param) {
        Page pageContext = getPageContext();
        IPage<EquipmentGroupResult> page = this.baseMapper.customPageList(pageContext,param);
        return new PageResult<>(page);
    }

    @Override
    public EquipmentCategory detail(Long eid) {
        return this.getById(eid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEquipmentCategory(EquipmentGroupParam param) {
        try {
            equipmentCategoryMapper.update(param);
        } catch (Exception e) {
            throw new ServiceException(MdmExceptionEnum.ENABLED_ERROR.getCode(),"编辑失败!");
        }

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long eid) {
        QueryWrapper<EquipmentCategory> gQueryWrapper = new QueryWrapper<>();
        gQueryWrapper.eq("eparentid", eid);
        Integer gcount = this.baseMapper.selectCount(gQueryWrapper);
        if (gcount >= 1){
            throw new ServiceException(MdmExceptionEnum.GROUPHAS_ERROR.getCode(),"分组下存在数据!");
        }else{
            QueryWrapper<Facility> wrapper = new QueryWrapper<>();
            wrapper.eq("fgroupid",eid);
            Integer integer = facilityMapper.selectCount(wrapper);
            if (integer >= 1){
                throw new ServiceException(MdmExceptionEnum.GROUPHAS_ERROR.getCode(),"分组下存在数据");
            }else{
                try {
                    this.baseMapper.deleteById(eid);
                } catch (Exception e) {
                    throw new ServiceException(MdmExceptionEnum.ENABLED_ERROR.getCode(),"删除失败");
                }
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addEquipmentCategory(EquipmentGroupParam param) {
        NumberUtil.checkRepeatNumber(this.baseMapper,param.getEnumber(),param.getEid());
        EquipmentCategory category = getEntity(param);
        this.save(category);
    }

    private EquipmentCategory getEntity(EquipmentGroupParam param) {
        EquipmentCategory category =new EquipmentCategory();
        BeanUtil.copyProperties(param, category);
        return category;
    }

    private Page getPageContext() {
        return PageFactory.defaultPage();
    }
}
